﻿#region 版权信息

//  ------------------------------------------------------------------------------
//   Copyright (C) 成都联宇创新科技有限公司 版权所有。 
//   
//   文件名：LotterySweepstake.cs
//   文件功能描述：LotterySweepstake.cs
//   创建标识：骆智慧 2014/07/16
//   
//   修改标识：
//   修改描述：
//  ------------------------------------------------------------------------------

#endregion

#region 名称空间导入

using System;
using System.Collections.Generic;
using Castle.ActiveRecord;

#endregion

namespace ECity.Domain
{
    /// <summary>
    ///     抽奖活动
    /// </summary>
    [Serializable]
    [ActiveRecord(Tables.LotterySweepstake)]
    public class LotterySweepstake : ActiveRecordBase<LotterySweepstake>
    {
        /// <summary>
        ///     编号
        /// </summary>
        [PrimaryKey(PrimaryKeyType.Sequence, SequenceName = Sequences.LotterySweepstake)]
        public virtual int ID { get; set; }

        /// <summary>
        ///     活动代码
        /// </summary>
        [Property(Column = "CODE", NotNull = true)]
        public virtual string Code { get; set; }

        /// <summary>
        ///     抽奖活动名称
        /// </summary>
        [Property(Column = "TITLE", NotNull = true)]
        public virtual string Title { get; set; }

        /// <summary>
        ///     活动描述
        /// </summary>
        [Property(Column = "DESCRIPTION")]
        public virtual string Description { get; set; }

        /// <summary>
        ///     开始时间
        /// </summary>
        [Property(Column = "START_DATE", NotNull = true)]
        public virtual DateTime StartDate { get; set; }

        /// <summary>
        ///     结束日期
        /// </summary>
        [Property(Column = "END_DATE", NotNull = true)]
        public virtual DateTime EndDate { get; set; }

        /// <summary>
        ///     是否启用活动
        /// </summary>
        [Property(Column = "IS_ACTIVED", NotNull = true)]
        public virtual int IsActived { get; set; }

        /// <summary>
        ///     是否正在更新活动数据 1 正在更新 0 没有更新 （状态 1 不允许开出奖项）
        /// </summary>
        [Property(Column = "IS_UPDATING", NotNull = true)]
        public virtual int IsUpdating { get; set; }

        /// <summary>
        ///     本活动参与玩家
        /// </summary>
        [HasMany(typeof (LotteryRecord), Lazy = true, ColumnKey = "SWEEPSTAKE_ID", Table = Tables.LotteryRecord)]
        public virtual IList<LotteryRecord> Players { get; set; }

        /// <summary>
        ///     本活动玩家中奖纪录
        /// </summary>
        [HasMany(typeof (LotteryRecord), Lazy = true, ColumnKey = "SWEEPSTAKE_ID", Table = Tables.LotteryRecord,
            Where = "PLAY_RESULT=1")]
        public virtual IList<LotteryRecord> AllWinPlayers { get; set; }

        /// <summary>
        ///     本活动今日中奖玩家纪录
        /// </summary>
        [HasMany(typeof (LotteryRecord), Lazy = true, ColumnKey = "SWEEPSTAKE_ID", Table = Tables.LotteryRecord,
            Where = "PLAY_RESULT=1 AND TO_CHAR(PLAY_TIME,'YYYYMMDD')= TO_CHAR(SYSDATE,'YYYYMMDD') ")]
        public virtual IList<LotteryRecord> TodayWinPlayers { get; set; }

        /// <summary>
        ///     本活动所有的奖品列表
        /// </summary>
        [HasMany(typeof (LotteryPrize), ColumnKey = "SWEEPSTAKE_ID", Table = Tables.LotteryRecord)]
        public virtual IList<LotteryPrize> AllPrizes { get; set; }

        /// <summary>
        ///     本活动的激活的奖品列表
        /// </summary>
        [HasMany(typeof (LotteryPrize), ColumnKey = "SWEEPSTAKE_ID", Table = Tables.LotteryRecord,
            Where = "IS_ACTIVED=1")]
        public virtual IList<LotteryPrize> ActivedPrizes { get; set; }

        /// <summary>
        ///     本活动的全部奖项配置
        /// </summary>
        [HasMany(typeof (LotteryConfig), ColumnKey = "SWEEPSTAKE_ID", Table = Tables.LotteryRecord)]
        public virtual IList<LotteryConfig> AllPrizeConfigs { get; set; }

        /// <summary>
        ///     本活动今日可用的奖项配置
        /// </summary>
        [HasMany(typeof (LotteryConfig), ColumnKey = "SWEEPSTAKE_ID", Table = Tables.LotteryRecord,
            Where =
                "IS_ACTIVED=1 AND TO_CHAR(START_DATE,'YYYYMMDD') &lt;=TO_CHAR(SYSDATE,'YYYYMMDD') AND TO_CHAR(END_DATE,'YYYYMMDD') &gt;= TO_CHAR(SYSDATE,'YYYYMMDD')"
            )]
        public virtual IList<LotteryConfig> TodayActivedPrizeConfigs { get; set; }

        /// <summary>
        ///     刷新活动配置
        /// </summary>
        public virtual void RefreshConfig()
        {
            using (var scope = new SessionScope())
            {
                foreach (var item in AllPrizeConfigs)
                {
                    //开始日期小于或等于今天
                    var startDateLesserOrEqualThanToday = DateTime.Compare(item.StartDate, DateTime.Today) < 1;
                    //结束日期大于或等于今天
                    var endDateGreaterOrEqualThanToday = DateTime.Compare(item.EndDate, DateTime.Today) > -1;

                    if (startDateLesserOrEqualThanToday && endDateGreaterOrEqualThanToday)
                    {
                        item.IsActived = 1;
                    }
                    else
                    {
                        item.IsActived = 0;
                    }

                    item.Update();
                }
            }
            Refresh();
        }

        public override int GetHashCode()
        {
            return Tables.LotterySweepstake.GetHashCode() ^ ID;
        }
    }
}